import { MyGameObject } from "./MyGameObject";
import { Cell } from "./ceil";
export class Snake extends MyGameObject{

    constructor(info,gamemap){
        super();

        this.id = info.id;
        this.color = info.color;
        this.gamemap = gamemap;

        this.cells = [new Cell(info.r,info.c)];//蛇的身体，cells【0】是蛇头

        this.speed = 10; //每秒移动的格子
        this.status = "stop";//stop静止，die失败,move 移动。

        this.time = 0;//回合时间

        this.food = gamemap.food;
        this.d = 0;

        this.last_head_x = this.cells[0].c;
        this.last_head_y = this.cells[0].r;

        this.start_record = false;//来判断record时有没有启动


        this.send_count = 0;//向云端发送信息次数

        this.eye_way = 0;
        this.eye_dx =[//蛇眼睛不同方向的偏移量
            [-1,1],
            [1,1],
            [1,-1],
            [-1,-1]
        ]
        this.eye_dy =[
            [-1,-1],
            [-1,1],
            [1,1],
            [1,-1]
        ]

    }

    start() {
    }


    set_step(d){ //记录或者改变下一步
        this.d = d;
        // console.log(this.gamemap.store.state.record.a_step);
        if(this.id != this.gamemap.store.state.user.userId)
        {
            switch(d){
            case 0:
                d = 1;
                break;
            case 1:
                d = 0
                break;
            case 2:
                d = 3;
                break;
            case 3:
                d = 2;
                break;
            }
            switch(d){
                case '0':
                    d = '1';
                    break;
                case '1':
                    d = '0'
                    break;
                case '2':
                    d = '3';
                    break;
                case '3':
                    d = '2';
                    break;
            }
        }
        switch(d){
            case 0:
                this.cells[0].y_way_next = -1;
                this.cells[0].x_way_next = 0;
                break;
            case 1:
                this.cells[0].y_way_next = 1;
                this.cells[0].x_way_next = 0
                break;
            case 2:
                this.cells[0].x_way_next = -1;
                this.cells[0].y_way_next = 0;
                break;
            case 3:
                this.cells[0].x_way_next = 1;
                this.cells[0].y_way_next = 0;
                break;
        }
        switch(d){
            case '0':
                this.cells[0].y_way_next = -1;
                this.cells[0].x_way_next = 0;
                break;
            case '1':
                this.cells[0].y_way_next = 1;
                this.cells[0].x_way_next = 0
                break;
            case '2':
                this.cells[0].x_way_next = -1;
                this.cells[0].y_way_next = 0;
                break;
            case '3':
                this.cells[0].x_way_next = 1;
                this.cells[0].y_way_next = 0;
                break;
        }
        if(this.status === "die")
        return;
        if(d === 4)
        {
            this.status = "stop";
        }
        else{
            this.status = "move"
        }
    }
///////////////////////////////
update_move_change()
{
            for(let i = this.cells.length - 1;i>=0;i--)//移动的各种判断以及改变方向
            {
                this.cells[i].x = parseFloat(Math.round(this.cells[i].x-0.5)) + 0.5;
                this.cells[i].y = parseFloat(Math.round(this.cells[i].y-0.5)) + 0.5;
                if(i !== 0)
                {
                    this.cells[i].x_way_now = this.cells[i-1].x_way_now;//从后往前，尾部的下一步行动方向等于上一个的行动方向
                    this.cells[i].y_way_now = this.cells[i-1].y_way_now;
                }
                else
                {
                        this.cells[i].x_way_now = this.cells[i].x_way_next;
                        this.cells[i].y_way_now = this.cells[i].y_way_next;
                }

            }
            let cellsx_round = Math.round(this.cells[0].x-0.5);
            let cellsy_round = Math.round(this.cells[0].y-0.5);

            if(this.gamemap.real_time_map[cellsy_round][cellsx_round] !== 1){
                if(this.gamemap.Snake[0].id === this.id){
                    this.gamemap.real_time_map[cellsy_round][cellsx_round] = 4;//芝士我的蛇头
                }
                else {
                    this.gamemap.real_time_map[cellsy_round][cellsx_round] = 3;//芝士敌人蛇头
                }
                
                    
            }
            if(cellsx_round !== this.last_head_x || cellsy_round !== this.last_head_y){
                if(this.cells.length > 3)
                    this.gamemap.real_time_map[this.last_head_y][this.last_head_x] = 1;//蛇头走掉了
                if(this.cells.length === 1)
                    this.gamemap.real_time_map[this.last_head_y][this.last_head_x] = 0;//蛇头真的走掉了
            }
            
            this.last_head_x = cellsx_round;
            this.last_head_y = cellsy_round;//上一个蛇头的地址
            if(this.cells.length !== 1)
            this.gamemap.real_time_map[Math.round(this.cells[this.cells.length-1].y-0.5)][Math.round(this.cells[this.cells.length-1].x-0.5)] = 0;//芝士蛇尾

            for(let i in this.food){//判断是否吃到了食物，目前直接遍历，后续会用算法优化。
                
                let r = this.food[i].r;
                let c = this.food[i].c;
                if(Math.abs(this.cells[0].x-c - 0.5)<0.5&&Math.abs(this.cells[0].y - r -0.5)<0.5)
                {
                    this.gamemap.walls_pleace[this.food[i].r][this.food[i].c] = false;//这是游戏中墙和food的地址
                    this.gamemap.real_time_map[this.food[i].r][this.food[i].c] = 0;//这是游戏中墙和food的地址
                    this.food[i].destroy();
                    this.food.splice(i,1);
                    this.cells.push(new Cell(this.cells[this.cells.length - 1].y - 0.5,this.cells[this.cells.length - 1].x - 0.5));
                    break;
                }
            }

            if(this.cells[0].x_way_now === 0)//判断蛇目前的前进方向，用来画眼睛方向
            {
                if(this.cells[0].y_way_now === 1)this.eye_way = 2;
                else if(this.cells[0].y_way_now === -1)this.eye_way = 0;
            }
            else
            {
                if(this.cells[0].x_way_now === 1)this.eye_way = 1
                else
                this.eye_way = 3;
            }
}
    update_move(){

        if(this.status !== "move"){
            return;
        }

        if(this.gamemap.is_bomb(this.cells[0]))//检测碰撞,这里会出现死亡
        {
            if(this.cells.length!==1)
            {
                this.cells[1].x_way_next = this.cells[0].x_way_next;
                this.cells[1].y_way_next = this.cells[0].y_way_next;

                this.cells.splice(0,1);
            }
            else
            {
                this.status = "die";
                if(this.id == this.gamemap.store.state.user.id && !this.gamemap.store.state.record.is_record)
                this.gamemap.store.state.pk.socket.send(JSON.stringify({
                    event: "die",
                    cells: this.gamemap.Snake[1].cells.length,
                }))
            }
            

        }


        let last_x_speace = Math.round(this.cells[0].x);
        let last_x = this.cells[0].x;
        let last_y_speace = Math.round(this.cells[0].y);
        let last_y = this.cells[0].y;
        for(let i in this.cells)
        {
            // console.log(this.speed * this.timedelta * this.cells[i].y_way_now);
            this.cells[i].x += this.speed * this.timedelta * this.cells[i].x_way_now / 1000;//每一帧的时间间隔乘以速度加速度方向向量即可移动
            this.cells[i].y += this.speed * this.timedelta * this.cells[i].y_way_now / 1000;

        }

        if(this.id != this.gamemap.store.state.user.id && !this.gamemap.store.state.record.is_record){
            return;
        }
        let map = null;
        if(Math.abs(Math.abs(Math.round(this.cells[0].x) - this.cells[0].x) - 0.5) < 0.05 && Math.abs(Math.abs(Math.round(this.cells[0].y) - this.cells[0].y) - 0.5) < 0.06)
        {
            if(this.gamemap.store.state.pk.botId !== -1)map = this.gamemap.real_time_map;//判断机器人
            if(!this.gamemap.store.state.record.is_record)//判断是不是回放，不是回放才能发消息
            {
            this.gamemap.store.state.pk.socket.send(JSON.stringify({
                        event: "move",
                        direction: this.d,
                        gameMap:map,
                    }))
                    if(this.send_count === 0){
                        setTimeout(function() {
                        }, 1500);
                    }
                    this.send_count ++ ;

                }
            else{
                if(this.gamemap.Snake[0] === this){
                    if(this.start_record||this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a >= this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b){
                        if(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a] !== null)
                            this.set_step(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a++]);
                            this.update_move_change();
                            this.start_record = true;
                            
                        

                    }
                }
                else{
                    // console.log(this.gamemap.step_count_a);
                    // this.gamemap.step_count_a++;
                    if(this.start_record||this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b >= this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a){
                        if(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b] !== null)
                            this.set_step(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b++]);
                            this.update_move_change();
                            this.start_record = true;

                        
                    }
                }
            }
            // this.update_move_change();
        }
        else if(Math.abs(Math.abs(Math.round(this.cells[0].x) - this.cells[0].x) - 0.5) < 0.05 && (Math.round(this.cells[0].y)!==last_y_speace) && !Math.abs(Math.abs(last_y - last_y_speace) - 0.5) < 0.01)//判断是否跨过了中点。
        {//这里步长太短，没法板回来
            if(this.gamemap.store.state.pk.botId !== -1)map = this.gamemap.real_time_map;
            if(!this.gamemap.store.state.record.is_record)
            this.gamemap.store.state.pk.socket.send(JSON.stringify({
                event: "move",
                direction: this.d,
                gameMap:map,
            }))
            else{
                if(this.gamemap.Snake[0] === this){
                    if(this.start_record||this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a >= this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b){
                        if(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a] !== null)
                            this.set_step(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a++]);
                            this.update_move_change();
                            
                        
                        
                    }
                }
                else{
                    if(this.start_record||this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b >= this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a){
                        if(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b] !== null)
                            this.set_step(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b++]);
                            this.update_move_change();

                    }
                }
            }
            // this.update_move_change();
        }
        else if(Math.abs(Math.abs(Math.round(this.cells[0].y) - this.cells[0].y) - 0.5) < 0.05 && (Math.round(this.cells[0].x)!== last_x_speace)&& !Math.abs(Math.abs(last_x - last_x_speace) - 0.5) < 0.01)//上面同理
        {
            if(this.gamemap.store.state.pk.botId !== -1)map = this.gamemap.real_time_map;
            if(!this.gamemap.store.state.record.is_record)
            this.gamemap.store.state.pk.socket.send(JSON.stringify({
                event: "move",
                direction: this.d,
                gameMap:map,
            }))
            else{
                if(this.gamemap.Snake[0] === this){
                    if(this.start_record||this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a >= this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b){
                        if(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a] !== null)
                            this.set_step(this.gamemap.store.state.record.a_step[this.gamemap.step_count_a++]);
                            this.update_move_change();
                            
                        
                        
                    }
                }
                else{
                    if(this.start_record||this.gamemap.store.state.record.b_step.length - this.gamemap.step_count_b >= this.gamemap.store.state.record.a_step.length - this.gamemap.step_count_a){
                        if(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b] !== null)
                            this.set_step(this.gamemap.store.state.record.b_step[this.gamemap.step_count_b++]);
                            this.update_move_change();
                            
                        
                        
                    }
                }
            }
            // this.update_move_change();
        }

    }
    ///////////////////////////////////////////////
    update(){//每一帧执行一次
        this.render();
        this.update_move();
    }

    render(){
        const L = this.gamemap.L;
        const ctx = this.gamemap.ctx;

        ctx.fillStyle = this.color;
        if(this.status === "die")
        ctx.fillStyle = "#123456";

        for(const cell of this.cells)
        {
            ctx.beginPath();
            ctx.arc(cell.x*L,cell.y*L,L/2,0,Math.PI *2);
            ctx.fill();
        }
        // for(let i = 0;i<this.cells.length-1;i++)//将蛇变得连起来
        // {
        //     if(Math.abs(this.cells[i].x - this.cells[i+1].x) < 0.1)
        //     {
        //         ctx.fillRect((this.cells[i].x - 0.5)*L,Math.min(this.cells[i].y,this.cells[i+1].y)*L,L,L);
        //     }
        //     else
        //     {
        //         ctx.fillRect(Math.min(this.cells[i].x,this.cells[i+1].x)*L,(this.cells[i].y - 0.5)*L,L,L);
        //     }
        // }


        ctx.fillStyle = "black";
        for(let i =0;i<2;i++)
        {
            const eye_x = this.cells[0].x + this.eye_dx[this.eye_way][i]*0.25;
            const eye_y = this.cells[0].y + this.eye_dy[this.eye_way][i]*0.25;
            ctx.beginPath();
            ctx.arc(eye_x * L, eye_y * L,L*0.1,0,Math.PI*2);
            ctx.fill();

        }
    }
}